﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms.Skin.Design
{
    /// <summary>
    /// 
    /// </summary>
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public class TabControlDesigner : System.Windows.Forms.Design.ParentControlDesigner
    {
        internal protected TabControl TabControl => (TabControl)base.Control;

        protected ISelectionService SelectionService => GetService(typeof(ISelectionService)) as ISelectionService;

        protected bool IsSelected
        {
            get
            {
                if (SelectionService?.GetComponentSelected(this.Component) == true)
                    return true;
                return false;
            }
        }
        protected override bool GetHitTest(Point point)
        {
            if (!IsSelected)
                return false;
            Point p = this.Control.PointToClient(point);
            foreach (TabPage tabPage in this.TabControl.TabPages)
            {
                var bounds = this.TabControl.GetTabPageBounds(tabPage);
                if (bounds != default && bounds.Contains(p))
                {
                    return true;
                }
            }
            return base.GetHitTest(point);
        }

        protected override void OnDragEnter(DragEventArgs de)
        {
            base.OnDragEnter(de);
            de.Effect = DragDropEffects.None;
        }

        public override System.ComponentModel.Design.DesignerVerbCollection Verbs
        {
            get
            {
                if (m_Verbs == null)
                {
                    m_Verbs = new System.ComponentModel.Design.DesignerVerbCollection();
                    m_Verbs.Add(new System.ComponentModel.Design.DesignerVerb("添加", this.AddPage));
                }
                return m_Verbs;
            }
        }
        private System.ComponentModel.Design.DesignerVerbCollection m_Verbs { get; set; }



        public override void InitializeNewComponent(IDictionary defaultValues)
        {
            base.InitializeNewComponent(defaultValues);
            for (int i = 0; i < 3; i++)
            {
                this.AddPage(this.Control, EventArgs.Empty);
            }
            this.TabControl.SelectedTab = this.TabControl.TabPages[0];
        }

        protected void AddPage(object sender, EventArgs e)
        {
            TabPage page = this.TabControl.TabPages.Add();
            if (page.Site is not null)
                page.Text = page.Site.Name;
        }      
    }
}
